package com.originskyseed.platform.cms.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.ProductInfoTemplate;
import com.originskyseed.platform.cms.service.IProductInfoTemplateService;
import com.originskyseed.platform.cms.vo.request.ProductInfoTemplateRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 产品模板信息表Controller类（OSI_PRODUCT_INFO_TEMPLATE）
 * 
 * @author Tangwanghui
 */
@Controller
@RequestMapping("/productInfoTemplate")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class ProductInfoTemplateController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(ProductInfoTemplateController.class);

	@DubboReference
	private IProductInfoTemplateService productInfoTemplateService;

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/productInfoTemplateList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<ProductInfoTemplate>> productInfoTemplateList(HttpServletRequest request) {
		BaseResponseBody<BasePage<ProductInfoTemplate>> result = null;
		try {
			// 当前用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			ProductInfoTemplateRequestVO requestVO = this.getParameterBean(ProductInfoTemplateRequestVO.class);

			// 分页查询
			BasePage<ProductInfoTemplate> page = new BasePage<ProductInfoTemplate>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.productInfoTemplateService.selectListByPage(requestVO.toMap(), page);
			for (ProductInfoTemplate productInfoTemplate:page.getResults()) {
				if(productInfoTemplate.getIsDefault()==1 && !userInfo.getId().equals(productInfoTemplate.getUserId())){
					productInfoTemplate.setIsDefault(0);
				}
			}
			result = new BaseResponseBody<BasePage<ProductInfoTemplate>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<ProductInfoTemplate>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/productInfoTemplateSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<ProductInfoTemplate> productInfoTemplateSave(HttpServletRequest request) {
		BaseResponseBody<ProductInfoTemplate> result = null;
		try {
			// 当前登录信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 请求对象
			ProductInfoTemplate productInfoTemplate = this.getParameterBean(ProductInfoTemplate.class);

			if (StringUtils.isNotBlank(productInfoTemplate.getId())) {
				this.productInfoTemplateService.updateByPrimaryKey(productInfoTemplate);
			} else {
				productInfoTemplate.setIsDefault(0);
				productInfoTemplate.setIsDel(0);
				productInfoTemplate.setCreatorId(userInfo.getId());
				productInfoTemplate.setCreator(userInfo.getName());
				this.productInfoTemplateService.insertSelective(productInfoTemplate);
			}
			result = new BaseResponseBody<ProductInfoTemplate>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), productInfoTemplate);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<ProductInfoTemplate>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 * 
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/productInfoTemplateDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> productInfoTemplateDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});

			this.productInfoTemplateService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 设置默认
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/updateDefault.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody updateDefault(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 当前用户信息
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);

			// 请求参数
			String id = this.getParameterString("id", null);

			this.productInfoTemplateService.updateIsDefault(id,userInfo.getId(),1);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 查询列表
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/selectProductInfoTemplateList.json", method = RequestMethod.POST)
	public BaseResponseBody selectProductInfoTemplateList(HttpServletRequest request) {
		BaseResponseBody result = null;
		try {
			// 请求对象
			ProductInfoTemplate parameterBean = this.getParameterBean(ProductInfoTemplate.class);

			// 列表查询
			List<ProductInfoTemplate> productInfoTemplateList = this.productInfoTemplateService.selectList(parameterBean);
			result = new BaseResponseBody(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), productInfoTemplateList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}